/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

/*
 * Created on 4/07/2004
 *
 */

package au.com.lastweekend.jim.io.jpeg;

import java.util.HashMap;
import java.util.Map;

import au.com.lastweekend.jim.io.metadata.MetadataException;

/**
 * Represents data contained in Jpeg SOF segment. Strictly there is no need to store the frame values as Tags, but it might help
 * with debugging and for others to understand the JPEG spec.
 * 
 * @version $Id: JpegStartOfFrameDirectory.java,v 1.3 2006/02/20 12:21:10 ggardner Exp $
 * @author grant@lastweekend.com.au
 * 
 */
public class JpegStartOfFrameDirectory extends RawJpegSegmentDirectory {

    public static final int TAG_FRAME_PRECISION = 1;
    public static final int TAG_FRAME_WIDTH = 2;
    public static final int TAG_FRAME_HEIGHT = 3;
    public static final int TAG_FRAME_COMPONENTS = 4;
    public static final int TAG_COMPONENT_IDS = 5;
    public static final int TAG_VERTICAL_SAMPLING_FACTORS = 6;
    public static final int TAG_HORIZONTAL_SAMPLING_FACTORS = 7;
    public static final int TAG_QUANTISATION_TABLES = 8;

    private static final HashMap<Integer, String> TAG_NAME_MAP = new HashMap<Integer, String>();
    static {
        TAG_NAME_MAP.put(TAG_FRAME_PRECISION, "Frame Precision");
        TAG_NAME_MAP.put(TAG_FRAME_WIDTH, "Frame Width");
        TAG_NAME_MAP.put(TAG_FRAME_HEIGHT, "Frame Height");
        TAG_NAME_MAP.put(TAG_FRAME_COMPONENTS, "Number of Components");
        TAG_NAME_MAP.put(TAG_COMPONENT_IDS, "Component IDs");
        TAG_NAME_MAP.put(TAG_VERTICAL_SAMPLING_FACTORS, "Vertical Sampling Factors");
        TAG_NAME_MAP.put(TAG_HORIZONTAL_SAMPLING_FACTORS, "Horizontal Sampling Factors");
        TAG_NAME_MAP.put(TAG_QUANTISATION_TABLES, "Quantisation table id");

    }

    @Override
    protected Map<Integer, String> getTagNameMap() {

        return TAG_NAME_MAP;
    }

    /**
     * @param segment
     */
    public JpegStartOfFrameDirectory(JpegSegment segment) {

        super(segment);
    }

    @Override
    public String getName() {

        return "Jpeg Start of Frame";
    }

    public byte getNumberOfComponents() {

        return ((Byte) getObject(TAG_FRAME_COMPONENTS)).byteValue();
    }

    public byte getComponentID(int i) {

        return getByteArrayValue(TAG_COMPONENT_IDS, i);

    }

    public byte getHorizontalSamplingFactor(int i) {

        return getByteArrayValue(TAG_HORIZONTAL_SAMPLING_FACTORS, i);
    }

    public byte getVerticalSamplingFactor(int i) {

        return getByteArrayValue(TAG_VERTICAL_SAMPLING_FACTORS, i);
    }

    public int getFrameWidth() throws JpegProcessingException {

        try {
            return getInt(TAG_FRAME_WIDTH);
        } catch (MetadataException e) {
            throw new JpegProcessingException(e);
        }
    }

    public int getFrameHeight() throws JpegProcessingException {

        try {
            return getInt(TAG_FRAME_HEIGHT);
        } catch (MetadataException e) {
            throw new JpegProcessingException(e);
        }
    }

    public byte getFramePrecisison() throws JpegProcessingException {

        try {
            return (byte) getInt(TAG_FRAME_PRECISION);
        } catch (MetadataException e) {
            throw new JpegProcessingException(e);
        }
    }

    public byte getQuantisationTable(int i) {

        return getByteArrayValue(TAG_QUANTISATION_TABLES, i);
    }
}